package com.wanmait.recycle.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wanmait.recycle.pojo.Admin;
import com.wanmait.recycle.pojo.Userinfo;
import com.wanmait.recycle.service.UserinfoService;
import com.wanmait.recycle.util.EmailSendByMessage;

@Controller
@RequestMapping("/manager/userinfo")
public class UserinfoController {

	@Resource
	private UserinfoService userinfoService;

	// user管理列表
	@RequestMapping("userList")
	public ModelAndView userList() {
		ModelAndView mv = new ModelAndView();

		Userinfo userinfo = new Userinfo();
		userinfo.setEnable(true);
		List<Userinfo> userinfos = userinfoService.findAllByExample(userinfo);

		mv.addObject("userinfos", userinfos);

		return mv;
	}

	// 已经删除的用户
	@RequestMapping("userDel")
	public ModelAndView userDel() {

		ModelAndView mv = new ModelAndView();

		Userinfo userinfo = new Userinfo();
		userinfo.setEnable(false);
		List<Userinfo> userinfos = userinfoService.findAllByExample(userinfo);

		mv.addObject("userinfos", userinfos);

		return mv;
	}

	// 用户上传的认证图片管理
	@RequestMapping("userApprovePic")
	@ModelAttribute("approveUserinfo")
	public List<Userinfo> ApprovePic() {		
		 return	userinfoService.findAllApprovePic();
	}

	// 个人信息
	@RequestMapping("userShow")
	public ModelAndView memberShow(Integer id) {
		ModelAndView mv = new ModelAndView();
		Userinfo userinfo = userinfoService.selectByPrimaryKey(id);
		mv.addObject("Muserinfo", userinfo);
		return mv;
	}

	// 公司信息
	@RequestMapping("companyShow")
	public ModelAndView companyShow(Integer id) {
		ModelAndView mv = new ModelAndView();
		Userinfo userinfo = userinfoService.selectByPrimaryKey(id);
		mv.addObject("Cuserinfo", userinfo);
		return mv;
	}
	
	// 公司信息
	@RequestMapping("companyPic")
	public ModelAndView companyPic(Integer id) {
		ModelAndView mv = new ModelAndView();
		Userinfo userinfo = userinfoService.selectByPrimaryKey(id);
		mv.addObject("Puserinfo", userinfo);
		return mv;
	}

	// 模糊搜索+时间段搜索
	@RequestMapping("searchUserinfo")
	public ModelAndView searchUserinfo(String searchStr, String beginTime, String endTime, String enable) {

		ModelAndView mv = new ModelAndView();
		if (searchStr == "") {
			searchStr = null;
		}

		List<Userinfo> userinfos = userinfoService.findAllBySearch(searchStr, beginTime, endTime, enable);
		Map<String, String> SearchMap = new HashMap<>();
		SearchMap.put("searchStr", searchStr);
		SearchMap.put("beginTime", beginTime);
		SearchMap.put("endTime", endTime);

		mv.addObject("userinfos", userinfos);
		mv.addObject("SearchMap", SearchMap);

		if (enable.equals("1")) {
			mv.setViewName("/manager/userinfo/userList");
		} else {
			mv.setViewName("/manager/userinfo/userDel");
		}

		return mv;
	}

	// 删除操作
	@RequestMapping("delete")
	@ResponseBody
	public void delete(String ids) {
		userinfoService.deleteByIds(ids);
	}

	// 删除操作
	@RequestMapping("delete1")
	@ResponseBody
	public void delete1(Integer id) {
		userinfoService.deleteById(id);
	}

	// 还原操作
	@RequestMapping("restore")
	@ResponseBody
	public void restore(String ids) {
		userinfoService.restoreByIds(ids);
	}

	// 还原操作
	@RequestMapping("restore1")
	@ResponseBody
	public void restore1(Integer id) {
		userinfoService.restoreById(id);
	}
	
	// 多人通过操作
	@RequestMapping("pass")
	@ResponseBody
	public void pass(String ids,HttpSession session) {	
		Admin admin = (Admin)session.getAttribute("admin");
		
		userinfoService.passByIds(ids,admin);
		
		List<Userinfo> userinfos = userinfoService.findMore(ids);
		for (Userinfo userinfo : userinfos) {
			String str = "您的验证已经通过";
			EmailSendByMessage byMessage = EmailSendByMessage.getEmailSend();
			try {
				byMessage.sendEmail(str, userinfo);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	// 通过操作
	@RequestMapping("pass1")
	@ResponseBody
	public String pass(Integer id,HttpSession session) {	
		Admin admin = (Admin)session.getAttribute("admin");
		userinfoService.passById(id,admin);
		
		Userinfo userinfo = userinfoService.findAllById(id);
		String str = "您的验证已经通过";
		EmailSendByMessage byMessage = EmailSendByMessage.getEmailSend();
		try {
			byMessage.sendEmail(str, userinfo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		String time = simpleDateFormat.format(System.currentTimeMillis()).toString();
		return time;
	}

	// 取消通过操作
	@RequestMapping("unpass")
	@ResponseBody
	public String unpass(Integer id,HttpSession session,String str) {
		Admin admin = (Admin)session.getAttribute("admin");
		userinfoService.unpassById(id,admin);
		
		Userinfo userinfo = userinfoService.findAllById(id);
		
		EmailSendByMessage byMessage = EmailSendByMessage.getEmailSend();
		try {
			byMessage.sendEmail(str, userinfo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		String time = simpleDateFormat.format(System.currentTimeMillis()).toString();
		return time;
	}
	
}
